package com.ipucao.android.testgeo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

public class MainActivity extends Activity {


    private LocationClient mLocationClient;
    public BDLocationListener myListener;
    private Button mButton;
    private TextView mTv;
    private Vibrator mVibrator01 = null;
    private boolean mIsStart;
    Button btnShowLocation;

    // GPSTracker class
    GPSTracker gps;
//    MapView mMapView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButton = (Button) findViewById(R.id.btnShowLocation);
        mTv = (TextView) findViewById(R.id.textView);
        mIsStart = false;

        mLocationClient = new LocationClient(this.getApplicationContext());
        myListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myListener);

        LocationClientOption locationOption = new LocationClientOption();

        locationOption.setOpenGps(true);
        locationOption.setCoorType("bd0911");

        locationOption.setPriority(LocationClientOption.NetWorkFirst);
        locationOption.setAddrType("all");
        locationOption.setProdName("BaiduLocation");

        locationOption.setScanSpan(5000);
        locationOption.disableCache(true);

        mLocationClient.setLocOption(locationOption);

        Log.i("Baidu", "BaiduMapMyLocationActivity 开启定位");
        mLocationClient.start();

        mButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (!mIsStart) {
                    mLocationClient.start();
                    mButton.setText("Stop");
                    mIsStart = true;

                } else {
                    mLocationClient.stop();
                    mIsStart = false;
                    mButton.setText("Start");
                }
            }

        });

//        SDKInitializer.initialize(getApplicationContext());
//        setContentView(R.layout.activity_main);

//        btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
//
//        // show location button click event
//        btnShowLocation.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View arg0) {
//                // create class object
//                gps = new GPSTracker(MainActivity.this);
//
//                // check if GPS enabled
//                if (gps.canGetLocation()) {
//
//                    double latitude = gps.getLatitude();
//                    double longitude = gps.getLongitude();
//
//                    // \n is for new line
//                    Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
//                } else {
//                    // can't get location
//                    // GPS or Network is not enabled
//                    // Ask user to enable GPS/network in settings
//                    gps.showSettingsAlert();
//                }
//
//            }
//        });
    }

    public class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location == null)
                return;
            StringBuffer sb = new StringBuffer(256);
            sb.append("time : ");
            sb.append(location.getTime());
            sb.append("\nerror code : ");
            sb.append(location.getLocType());
            sb.append("\nlatitude : ");
            sb.append(location.getLatitude());
            sb.append("\nlontitude : ");
            sb.append(location.getLongitude());
            sb.append("\nradius : ");
            sb.append(location.getRadius());
            if (location.getLocType() == BDLocation.TypeGpsLocation) {
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
            }
            mTv.setText(sb);
            logMsg(sb.toString());
        }

        public void onReceivePoi(BDLocation poiLocation) {
            if (poiLocation == null) {
                return;
            }
            StringBuffer sb = new StringBuffer(256);
            sb.append("Poi time : ");
            sb.append(poiLocation.getTime());
            sb.append("\nerror code : ");
            sb.append(poiLocation.getLocType());
            sb.append("\nlatitude : ");
            sb.append(poiLocation.getLatitude());
            sb.append("\nlontitude : ");
            sb.append(poiLocation.getLongitude());
            sb.append("\nradius : ");
            sb.append(poiLocation.getRadius());
            if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
                sb.append("\naddr : ");
                sb.append(poiLocation.getAddrStr());
            }
            mTv.setText(sb);
            logMsg(sb.toString());
        }
    }

    private void logMsg(String s) {
        System.out.println(s);
    }

    @Override
    public void onDestroy() {
        mLocationClient.stop();
        super.onDestroy();
    }


}
